package com.sam.im.heiqun.uis.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.UserEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.EventBusType;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class MyWalletActivity extends BaseSwipeBackActivity {

    private static final String TAG = MyWalletActivity.class.getSimpleName();
    @BindView(R.id.center_title)
    TextView centerTitle;
    @BindView(R.id.right_commit)
    TextView rightCommit;
    @BindView(R.id.money)
    TextView money;

    @BindView(R.id.recharge)
    LinearLayout recharge;
    @BindView(R.id.withdraw)
    LinearLayout withdraw;
    @BindView(R.id.payandsend)
    LinearLayout payandsend ;
    @BindView(R.id.getmoney)
    LinearLayout getmoney ;
    @BindView(R.id.left_back)
    ImageView backImg;
    private PGService mPGservice;


    @Override
    public int getContentViewId() {
        return R.layout.activity_my_wallet;
    }

    @Override
    protected String getTitleText() {
        centerTitle.setText(R.string.my_wallet);
        rightCommit.setText(R.string.detail);
        return getString(R.string.my_wallet);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        mPGservice = PGService.getInstance();
//        getBalance();
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onBaseEvent(final Integer refresh) {
        if (refresh == EventBusType.RESULT_PAY_WX_SUCCESS){

        }
    }

    @OnClick({R.id.left_back, R.id.withdraw, R.id.recharge, R.id.right_commit,R.id.payandsend,R.id.getmoney})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back:
                scrollToFinishActivity();
                break;
            case R.id.recharge: // 充值跳转
                startActivityForResult(SelectRechargeTypeActivity.class, 101);
                break;
            case R.id.withdraw: // 提现跳转
                startActivityForResult(SelectionOfPresentTypeActivity.class, 101);
                break;
            case R.id.right_commit: // 明细跳转
                startActivity(MyWalletDetailsActivity.class);
                break;
            case R.id.payandsend: {//付款
                //Intent intentQR = new Intent(MyWalletActivity.this, QRcode.class);
                Intent intentget = new Intent(MyWalletActivity.this, SetAmoutActivity.class);
                intentget.putExtra("key","pay");
                //intentQR.putExtra("img", ToolsUtils.getUser().getHeadUrl());
                //intentQR.putExtra("type",0);
                //intentQR.putExtra("qrString", "paycode");
                // startActivityForResult(intentQR,333);
                // startActivity(intentQR);
                startActivity(intentget);
                break;
            }
            case R.id.getmoney://收款

                Intent intentget = new Intent(MyWalletActivity.this,SetAmoutActivity.class);
               // intentget.putExtra("img", ToolsUtils.getUser().getHeadUrl());
                //intentQR.putExtra("type",0);
                //intentQR.putExtra("qrString","");
                // startActivityForResult(intentQR,333);
                intentget.putExtra("key","get");
                startActivity(intentget);
                break;
        }

    }


    /**
     * 网络获取余额
     */
    public void getBalance() {
        UserEntivity userEntivity = ToolsUtils.getUser();
        if (userEntivity != null) {
            String userID = userEntivity.getId() + "";
            mPGservice.getBalance(userID).subscribe(new AbsAPICallback<String>() {
                @Override
                protected void onResultError(ApiException ex) {
                    showToast(getResources().getString(R.string.net_visit_exception));
                }

                @Override
                public void onNext(String s) {
                    money.setText(s);
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 101:
//                getBalance();
                break;

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getBalance();
    }
}
